package com.gxuwz.medical.domain.person;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.gxuwz.medical.dao.PersonDao;
import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.vo.PageBean;

public class Person {
	private String ybcardno;
	private Family family;
	private String hnbh="01";// 户内编号        自动生成
	private String fullname;// 姓名             必填
	private String idcard;// 身份证号           必填
	private String relative;//与户主关系   儿子|女儿|配偶|父亲|母亲
	private String sex;// 性别       男|女
	private Date brithday;// 出生年月日 |根据身份证号自动生成
	private String nation;// 民族              汉族|壮族|...
	private String health;// 健康状况      健康|残疾
	private String culture;// 文化程度   小学及以下|初中|高中|专科|本科及以上
	private String attribute;// 人员属性
	private String farmer;// 农业人口     是|否
	private String marry;// 婚姻状态         已婚|未婚
	private String workunit;// 工作单位
	private String tel;// 联系电话
	private PersonDao persondao = new PersonDao();
	public Person() {
		super();
	}
	
	public Person(String fullname, String idcard, String relative, String sex,
			String nation, String health, String culture,
			String attribute, String farmer, String marry, String workunit,
			String tel){
		super();
		this.fullname = fullname;
		this.idcard = idcard;
		this.relative = relative;
		this.sex = sex;
		this.nation = nation;
		this.health = health;
		this.culture = culture;
		this.attribute = attribute;
		this.farmer = farmer;
		this.marry = marry;
		this.workunit = workunit;
		this.tel = tel;
	}
	
	
	public PageBean queryByFamilycode(String familycode, int page,int size) throws SQLException{
		return persondao.queryByFamilycode(familycode, page, size);
	}
	public void del(String idcard) throws SQLException {
		persondao.del(idcard);
		syncTable();
	}
	public void add() throws SQLException, ParseException{
		add(new DbUtil().getConn());
	}
	public void add(Connection conn) throws SQLException, ParseException{
		createBrithday();
		createHnbh();
		createYBcardno();
		persondao.add(this,conn);
		syncTable();
	}
	public void update() throws SQLException, ParseException{
		persondao.update(this);
		syncTable();
	}
	public Person query() throws SQLException {
		return persondao.query(ybcardno);
	}
	public List<Person> queryAll() throws SQLException {
		return persondao.queryAll();
	}
	public PageBean queryAll(int page,int size ) throws SQLException {
		return persondao.queryAll(page,size);
	}
	public void syncTable(){//人员信息变动后应该同步到数据库表
		int familynum=countPerson();
		int farmernum=countFarmer();
		try{
			DbUtil db = new DbUtil();
			String sql = "update family set familynum='"+familynum+"',farmernum='"+farmernum+"' where familycode='"+this.family.getFamilycode()+"'";
			if(familynum == 0){
				sql = "delete from family where familycode='"+this.family.getFamilycode()+"'";
			}
			db.executeUpdate(sql);
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	private int countPerson(){
		Connection conn = null;
		Statement stmt=null;
		ResultSet rs =null;
		int count = 0;
		try{
			conn = new DbUtil().getConn();
			stmt = conn.createStatement();
			String sql ="select count(*) from person where familycode='"+this.family.getFamilycode()+"'";
			rs = stmt.executeQuery(sql);
			if(rs.next()){
				count = rs.getInt(1);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return count;
	}
	private int countFarmer(){
		Connection conn = null;
		Statement stmt=null;
		ResultSet rs =null;
		int count = 0;
		try{
			conn = new DbUtil().getConn();
			stmt = conn.createStatement();
			String sql ="select count(*) from person where familycode='"+this.family.getFamilycode()+"' and farmer='是'";
			rs = stmt.executeQuery(sql);
			if(rs.next()){
				count = rs.getInt(1);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return count;
	}
	
	private void createYBcardno(){
		this.ybcardno = this.family.getFamilycode() + this.hnbh;
	}
	private void createHnbh()  {
		Connection conn = null;
		Statement stmt=null;
		ResultSet rs =null;
		try{
			conn = new DbUtil().getConn();
			stmt = conn.createStatement();
			String sql ="select max(hnbh) from person where familycode='"+this.family.getFamilycode()+"'";
			rs = stmt.executeQuery(sql);
			String hnbhstr="";
			if(rs.next()){
				hnbhstr = rs.getString(1);
			}
			if(hnbhstr==null)
				hnbhstr="00";
			int hnbh = Integer.parseInt(hnbhstr);
			hnbh++;
			this.hnbh=String.format("%02d", hnbh);
		}catch(SQLException e){
			e.printStackTrace();
		}
		
	}

	private void createBrithday() throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		brithday=df.parse(idcard.substring(6,10)+"-"+idcard.substring(10,12)+"-"+idcard.substring(12,14));
	}

	public Family getFamily() {
		return family;
	}
	public void setFamily(Family family) {
		this.family = family;
	}
	public String getHnbh() {
		return hnbh;
	}
	public void setHnbh(String hnbh) {
		this.hnbh = hnbh;
	}
	public String getFullname() {
		return fullname;
	}
	public void setFullname(String fullname) {
		this.fullname = fullname;
	}
	public String getIdcard() {
		return idcard;
	}
	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}
	public String getRelative() {
		return relative;
	}
	public void setRelative(String relative) {
		this.relative = relative;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Date getBrithday() {
		return brithday;
	}
	public void setBrithday(Date brithday) {
		this.brithday = brithday;
	}
	public String getNation() {
		return nation;
	}
	public void setNation(String nation) {
		this.nation = nation;
	}
	public String getHealth() {
		return health;
	}
	public void setHealth(String health) {
		this.health = health;
	}
	public String getCulture() {
		return culture;
	}
	public void setCulture(String culture) {
		this.culture = culture;
	}
	
	
	public String getAttribute() {
		return attribute;
	}
	public void setAttribute(String attribute) {
		this.attribute = attribute;
	}
	public String getFarmer() {
		return farmer;
	}
	public void setFarmer(String farmer) {
		this.farmer = farmer;
	}
	public String getMarry() {
		return marry;
	}
	public void setMarry(String marry) {
		this.marry = marry;
	}
	public String getWorkunit() {
		return workunit;
	}
	public void setWorkunit(String workunit) {
		this.workunit = workunit;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getYbcardno() {
		return ybcardno;
	}

	public void setYbcardno(String ybcardno) {
		this.ybcardno = ybcardno;
	}
	
}
